#include "sqliteoperate.h"

QSqlDatabase sqliteOperate::m_db;

sqliteOperate::sqliteOperate()
{

}

//创建数据库文件
bool sqliteOperate::createDataFile(const QString &strFileName)
{
	if(!QFile::exists(strFileName))//文件不存在，则创建
	{
		QDir fileDir = QFileInfo(strFileName).absoluteDir();
		QString strFileDir = QFileInfo(strFileName).absolutePath();
		if(!fileDir.exists()) //路径不存在，创建路径
		{
			fileDir.mkpath(strFileDir);
		}
		QFile dbFile(strFileName);
		if(!dbFile.open(QIODevice::WriteOnly))//未成功打开
		{
			dbFile.close();
			return false;
		}
		dbFile.close();
	}
	return true;
}

//打开数据库文件(Sqlite,*.db文件)
bool sqliteOperate::openDataBase(const QString& strFileName)
{
	m_db = QSqlDatabase::addDatabase("QSQLITE");
	m_db.setDatabaseName(strFileName);
	if(m_db.open())
	{
		return true;
	}
	return false;
}

//关闭数据库
void sqliteOperate::closeDataBase()
{
	m_db.close();
}

//判断数据库中是否存在该表
bool sqliteOperate::isExistTable(const QString& strTableName)
{
	QSqlQuery query;
	QString strSql = QString("SELECT 1 FROM sqlite_master where type = 'table' and  name = '%1'").arg(strTableName);
	query.exec(strSql);
	if(query.next())
	{
		int nResult = query.value(0).toInt();//有表时返回1，无表时返回null
		if(nResult)
		{
			return true;
		}
	}
	return false;
}

//判断表中是否含有某字段(列)
bool sqliteOperate::isExistField(const QString& strTableName, const QString& strFieldName)
{
	QSqlQuery query(m_db);
	QString strSql = QString("SELECT 1 FROM sqlite_master where type = 'table'"
							 " and name= '%1' and sql like '%%2%'").arg(strTableName).arg(strFieldName);
	query.exec(strSql);
	if(query.next())
	{
		int nResult = query.value(0).toInt();//有此字段时返回1，无字段时返回null
		if(nResult)
		{
			return true;
		}
	}
	return false;
}
